package sf.mapper;

import db.domain.User;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.Param;
import sf.common.wrapper.Page;
import sf.dao.sql.SQLUtils;
import sf.database.mapper.DaoMapper;
import sf.database.mapper.annotation.AutoSQL;
import sf.database.mapper.annotation.ExecuteProvider;
import sf.database.mapper.annotation.ExecuteSQL;
import sf.database.mapper.annotation.ExecuteTemplate;
import sf.database.mapper.annotation.OrmMapper;
import sf.database.mapper.annotation.OrmParam;
import sf.database.mapper.annotation.SelectKey;
import sf.database.mapper.annotation.SqlResource;
import sf.database.mapper.handle.RowLimit;
import sf.database.support.DMLType;

import java.util.List;
import java.util.Map;

@OrmMapper
@SqlResource("db.user")
public interface UserDAOMapper extends DaoMapper<User> {

    @ExecuteSQL(value = {"insert into wp_users(login_name,activation_key) values(?,?)"})
    void insertUser0(String loginName, String activeKey);

    @ExecuteSQL(value = {"insert into wp_users(login_name,activation_key) values(?,?)"})
    @SelectKey(keyColumn = "id", resultType = long.class)
    long insertUser(String loginName, String activeKey);

    @ExecuteSQL(value = {"insert into wp_users(login_name,activation_key) values(?,?)"})
    @SelectKey(keyColumn = "id", resultType = int.class)
    int insertUser2(String loginName, String activeKey);

    @ExecuteSQL(value = {"insert into wp_users(login_name,activation_key) values(?,?)"})
    @SelectKey(keyColumn = "id", resultType = Map.class)
    Map insertUser3(String loginName, String activeKey);

    @ExecuteSQL(value = {"select * from wp_users"})
    List<User> selectAll();

    @ExecuteSQL(value = {"select * from wp_users"})
    Page<User> selectSqlPage(RowLimit rl);

    @ExecuteSQL(value = {"select * from wp_users where id =?"})
    User selectSqlOne(int id);

    @ExecuteProvider(type = SQLUtils.class, method = "getByOne")
    User getBySqlOne(int id);

    @ExecuteTemplate(value = {"insert into wp_users(login_name,activation_key) values(#p(loginName),#p(activeKey))"}, type = DMLType.INSERT)
    void insertUserTemplate0(@OrmParam("loginName") String loginName, @OrmParam("activeKey") String activeKey);

    @ExecuteTemplate(value = {"insert into wp_users(login_name,activation_key) values(#p(loginName),#p(activeKey))"}, type = DMLType.INSERT)
    @SelectKey(keyColumn = "id", resultType = long.class)
    long insertUserTemplate(@OrmParam("loginName") String loginName, @OrmParam("activeKey") String activeKey);

    @ExecuteTemplate(value = {"insert into wp_users(login_name,activation_key) values(#p(loginName),#p(activeKey))"}, type = DMLType.INSERT)
    @SelectKey(keyColumn = "id", resultType = int.class)
    int insertUserTemplate2(@OrmParam("loginName") String loginName, @OrmParam("activeKey") String activeKey);

    @ExecuteTemplate(value = {"insert into wp_users(login_name,activation_key) values(#p(loginName),#p(activeKey))"}, type = DMLType.INSERT)
    @SelectKey(keyColumn = "id", resultType = Map.class)
    Map insertUserTemplate3(@OrmParam("loginName") String loginName, @OrmParam("activeKey") String activeKey);

    @ExecuteTemplate(value = {"select * from wp_users"})
    List<User> selectAllTemplate();

    @ExecuteTemplate(value = {"select #pageTag()* #end from wp_users"})
    Page<User> selectSqlPageTemplate(RowLimit rl);

    @ExecuteTemplate(value = {"select * from wp_users where id =#p(id)"})
    User selectSqlOneTemplate(@OrmParam("id") int id);

    @ExecuteTemplate(value = {"select * from wp_users where id =#p(id)"})
    User selectSqlOneTemplate(Map<String,Object> map);

    @ExecuteTemplate(id = "db.user.selectUserByTemplateId")
    List<User> selectAllTemplateId(@OrmParam("id") int id);

    @ExecuteTemplate
    List<User> selectUserByTemplateId(@OrmParam("id") int id);

    @AutoSQL
    List<User> findByIdOrderByLoginNameDesc(@Param("id") int id, Sort sort);

    @AutoSQL
    List<User> findDistinctUserByIdOrderByLoginNameDesc(@Param("id") int id);

    @AutoSQL
    Page<User> findByLoginNameLike(@Param("loginName") String loginName, Pageable pageable);

    @AutoSQL
    boolean existsById(@Param("id") int id);

    @AutoSQL
    long countByLoginNameLike(@Param("loginName") String loginName);

    @AutoSQL
    int deleteById(@Param("id") int id);
}
